package com.marvels.system.common.aop;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.dto.UserDTO;
import com.marvels.common.utils.CacheUtils;

/**
 * 
* @ClassName: ControllerAop 
* @Description: controller切面
* @author 李瑶
* @date 2018年1月18日 下午5:10:38 
*
 */
@Component
@Aspect
public class SystemControllerAop {
//	private static CommonParam commonParam;
	
	@Autowired
	RestTemplate restTemplate;
	
    @Pointcut("execution(* com.marvels.system..controller.*.*(..))")  
    public void controller(){}  
  
    @Around("controller()")
    public Object  around(ProceedingJoinPoint joinPoint) throws Throwable {  
    	//先取出当前url
        String methodName = joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName();
        Map  needPermissionMap = (Map)CacheUtils.getCacheObject("needPermissionMap");
        
        Object obj = null;
        if(needPermissionMap != null && needPermissionMap.get(methodName) != null) {
        	 UserDTO UserDTO = CacheUtils.getCacheUser();
     		Map<String,String> map = (Map<String,String>)CacheUtils.getCacheObject("urlPermission"+UserDTO.getUserCode());
     		if(map != null && map.get(methodName) == null) {
     			MarvelsResultDTO dto = new MarvelsResultDTO();
     			dto.setCode("401");
     			dto.setMessage("没有权限");
     			obj = dto;
     		}else//有权限
     			obj = joinPoint.proceed();
        }else
        	obj = joinPoint.proceed();
        return obj;
    	
    }  
}
